package com.xujf.sym.ui.base

import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import android.support.annotation.Nullable
import android.support.v7.widget.Toolbar
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.*
import com.xujf.sym.R
import com.xujf.sym.util.ResUtil
import kotlinx.android.synthetic.main.activity_base_simple.*

/**
 * Created by XuJiafeng on 2018/2/5.
 * Content：标准toolbarActivity
 */
abstract class BaseSimpleActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        }
        setUpUI(savedInstanceState)
    }

    abstract fun setUpUI(savedInstanceState: Bundle?)

    override fun setContentView(layoutResID: Int) {
        if (requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
        super.setContentView(R.layout.activity_base_simple)
        initTitleView()
        View.inflate(this, layoutResID, baseContent)
    }

    private fun initTitleView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//            window.statusBarColor = resources.getColor(R.color.compatible_translucent_status)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.statusBarColor = resources.getColor(R.color.compatible_translucent_status)
        }
        //标题栏+分割线
        var top = resources.getDimensionPixelOffset(R.dimen.title_bar_size) + ResUtil.dip2px(this, 0.5f)
        //状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            top += ResUtil.getStatusBarHeight(this)
        }
        (baseContent.layoutParams as FrameLayout.LayoutParams).topMargin = top

        toolbar.setNavigationOnClickListener (object : PerfectClickListener(){
            override fun onNoDoubleClick(v: View?) {
                finish()
            }
        })
    }

    /**
     * 设置图片会超出边界
     */
    fun setToolBarTopBg(resId: Int) {
        ll_toolbar.setBackgroundResource(resId)
    }

    fun translucentToolBar() {
        ll_toolbar.setBackgroundResource(R.color.translucent_half_status)
        line.visibility = View.GONE
        (baseContent.layoutParams as FrameLayout.LayoutParams).topMargin = 0
    }

    fun setRightText(text: String?, @Nullable icon: Int?, btnClick: View.OnClickListener) {
        rl_tv_right.visibility = View.VISIBLE
        if (text != null) {
            tv_right.text = text
        }
        if (icon != null) {
            tv_right.setBackgroundResource(icon.toInt())
//            val linearParams = btn_right.layoutParams
//            linearParams.height = StatusUtil.dip2px(this, 40f)
//            linearParams.width = StatusUtil.dip2px(this, 40f)
//            btn_right.layoutParams = linearParams
        }
        rl_tv_right.setOnClickListener(btnClick)
    }

    fun setRightImg(icon: Int?, btnClick: View.OnClickListener) {
        rl_iv_right.visibility = View.VISIBLE
        if (icon != null) {
            iv_right.setBackgroundResource(icon.toInt())
        }
        rl_iv_right.setOnClickListener(btnClick)
    }

    fun getBaseToolBar(): Toolbar {
        return toolbar
    }
}
